package cn.jiyun.service.impl;

import cn.jiyun.commons.ImgUtil;
import cn.jiyun.dao.domain.Stu;
import cn.jiyun.dao.domain.StuCount;
import cn.jiyun.dao.domain.StuVo;
import cn.jiyun.dao.mapper.StuMapper;
import cn.jiyun.service.StuService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StuServiceImpl implements StuService {

    @Autowired
    private StuMapper stuMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageInfo<StuVo> findLimit( Integer pageNum, StuVo stuVo ) {
        PageHelper.startPage(pageNum, 3);
        List<StuVo> list = stuMapper.findAllVO(stuVo);
        PageInfo<StuVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void saveStu( Stu stu ) {
        if (stu.getId() == null) {
            stu.setJoindate(new Date());//默认是当前日期
            stu.setStatus("0");//状态默认是待审核
            stu.setIsdelete("1");//做逻辑删除时使用
            stuMapper.insertSelective(stu);
        } else {
            stuMapper.updateByPrimaryKeySelective(stu);
        }
    }

    @Override
    public Stu findUpdate( Integer id ) {
        return stuMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteStu( Integer[] id ) {
        for (Integer key : id) {
            Stu stu = stuMapper.selectByPrimaryKey(key);
            stu.setIsdelete("0");
            stuMapper.updateByPrimaryKeySelective(stu);
        }
    }

    @Override
    public StuVo findShow( Integer id ) {
        StuVo stu = stuMapper.findShow(id);
        redisTemplate.opsForHash().putAll(stu.getId().toString(), JSONArray.parseObject(JSON.toJSONString(stu), Map.class));
        return stu;
    }

    @Override
    public List<StuCount> tongji() {
        return stuMapper.tongji();
    }

    @Override
    public void updateStatus( Stu stu ) {
        stuMapper.updateByPrimaryKeySelective(stu);
    }

    @Override
    public void outStu( HttpServletResponse response ) throws IOException {
        //创建表格
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建sheet页
        XSSFSheet sheet = workbook.createSheet();
        String[] tieles = {"姓名", "性别", "生日", "专业", "老师", "审批状态"};
        XSSFRow oneRow = sheet.createRow(0);
        for (int i = 0; i < tieles.length; i++) {
            oneRow.createCell(i).setCellValue(tieles[i]);
        }
        List<StuVo> list = stuMapper.findout();
        int index = 1;
        for (StuVo stuVo : list) {
            XSSFRow row = sheet.createRow(index);
            row.createCell(0).setCellValue(stuVo.getName());
            row.createCell(1).setCellValue(stuVo.getSex());
            row.createCell(2).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(stuVo.getBirthday()));
            row.createCell(3).setCellValue(stuVo.getMname());
            row.createCell(4).setCellValue(stuVo.getTname());
            if (stuVo.getStatus().equals("0")){
                row.createCell(5).setCellValue("待审核");
            }
            if (stuVo.getStatus().equals("1")){
                row.createCell(5).setCellValue("审批通过");
            }
            if (stuVo.getStatus().equals("2")){
                row.createCell(5).setCellValue("审批不通过");
            }
            index++;
        }
        ImgUtil.download(response,"p8期末考试导出.xlsx",workbook);

    }


}
